<?php

/**
 * Handles input data.
 * 
 * @param array $data
 * Dictionary with fields:
 *      mode = add|delete|update, 
 *      name
 *      text
 *      msgid
 * @param Guestbook $gb
 * @return list($postedMessage, $postedComment)
 * Incoming message and comment data for displaying it in forms
 * when some error happened.
 * @author Kirill Krasnoschekov
 */
function HandleData(array $data, Guestbook $gb)
{
    // Posted message/comment data
    // It will be pasted into corresponding forms if error occurs while adding.
    $postedMessage = array('name' => '', 'text' => '');
    $postedComment = array('msgid' => -1, 'name' => '', 'text' => '');
    // If left empty then all was ok and forms are shown empty    
    
    // Which mode is selected?    
    if ( isset($data['mode']) )
    {
        switch ( $data['mode'] ) 
        {
            // Adding message or comment
            case 'add':
                trace("Add mode");
                if ( isset($data['name']) AND isset($data['text']) )
                {
                    if ( isset($data['msgid']) )
                    {
                        // Add a comment
                        trace("Add comment mode");
                        $ok = $gb->AddComment((integer) $data['msgid'], 
                            prepareString($data['name']), 
                            prepareString($data['text']));
                            
                        // Error. Passing posted values to the corresponding form
                        if ( !$ok )
                        {
                            trace("Error.");
                            $postedComment['msgid'] = $data['msgid'];
                            $postedComment['name'] = $data['name'];
                            $postedComment['text'] = $data['text'];
                        }
                    }
                    else
                    {
                        // Add a message
                        trace("Add message mode");
                        $ok = $gb->AddMessage(
                            prepareString($data['name']), 
                            prepareString($data['text']));
    
                        // Error. Passing posted values to the corresponding form
                        if ( !$ok )
                        {
                            trace("Error.");
                            $postedMessage['name'] = $data['name'];
                            $postedMessage['text'] = $data['text'];
                        }
                    }
                }
                break;
        
            // Deleting message or comment
            case 'delete':
                trace("Delete mode");
                if ( isset($data['msgid']) )
                {
                    // Delete the message
                    trace("Delete message mode");
                    $gb->DeleteMessage((integer)$data['msgid']);
                } 
                elseif (isset($data['commentid']))
                { 
                    // Delete the comment
                    trace("Delete comment mode");
                    $gb->DeleteComment((integer)$data['commentid']);
                }
                break;
        
            // Updating the message
            case 'update':
                if ( isset($data['msgid']) AND
                     isset($data['name']) AND 
                     isset($data['text']) )
                {
                    trace("Update mode");
                    $gb->UpdateMessage(
                            (integer)$data['msgid'], 
                            prepareString($data['name']), 
                            prepareString($data['text']));
                }
                break;
            
            default:
                break;
        }
    } 

    // Returning incoming message and comment
    // (fields in which are left blank if all was ok)
    return array($postedMessage, $postedComment);
}



/**
 * Display all what should be displayed
 * 
 * @param Presentation $pr
 * @param Guestbook $gb
 * @param array $postedComment
 * @param array $postedMessage
 * @param int $page
 * @param int $timeStart
 * @author Kirill Krasnoschekov
 */
function Display
   (Presentation $pr, 
    Guestbook $gb, 
    array $postedMessage, 
    array $postedComment,
    $page, 
    $timeStart)
{
    // Header and message form
    $pr->PrintHeader();
    $pr->PrintMessageForm($postedMessage);
    
    // Fetching messages on the given page
    $messagesList = $gb->FetchMessages($page);
    
    foreach ( $messagesList as $i => $message ) 
    {
        // Printing the message
        $pr->PrintMessage($message);
        
        // Printing comments to this message
        $commentsList = $gb->FetchComments($message['id']);
        foreach ( $commentsList as $i => $comment ) 
        {
            $pr->PrintComment($comment);
        }
        
        // Printing comment form
        if ( $postedComment['msgid'] == $message['id'] )
        {
            // Filling form with old data
            $pr->PrintCommentForm($postedComment);
        }
        else 
        {
            // Making form empty
            $pr->PrintCommentForm(array('msgid' => $message['id'], 
                                        'name' => '', 
                                        'text' => ''));
        }
    }
    
    // Footer
    $pr->PrintFooter(microtime() - $timeStart);    
}

?>